﻿using Domain.Entity;
using Domain.Repository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EFCore.Repositorys
{
    public class EMRRepository : IEMRRepository
    {
        private readonly MyDbContext myDbContext;

        public EMRRepository(MyDbContext myDbContext)
        {
            this.myDbContext = myDbContext;
        }
        public async Task<int> AddEMR(EMR emr)
        {
            myDbContext.EMR.Add(emr);
            int result = await myDbContext.SaveChangesAsync();
            return result;
        }

        public async Task<int> DeleteEMR(int Id)
        {
            EMR emr = new EMR()
            {
                MreId = Id
            };
            myDbContext.EMR.Remove(emr);
            int result = await myDbContext.SaveChangesAsync();
            return result;
        }

        public async Task<List<EMR>> GetEMR()
        {
            return myDbContext.EMR.ToList();
        }

        public async Task<int> UpdateEMR(EMR emr)
        {
            EMR emr1 =myDbContext.EMR.Where(e => e.MreId == emr.MreId).FirstOrDefault();
            emr1.HumanAmount = emr.HumanAmount;
            emr1.Deadline = emr.Deadline;
            emr1.Changer = emr.Changer;
            emr1.ChangeTime = emr.ChangeTime;
            emr1.MajorDescribe = emr.MajorDescribe;
            emr1.EngageRequired = emr.EngageRequired;
            myDbContext.EMR.Update(emr1);
            int result =await myDbContext.SaveChangesAsync();
            return result;
        }
        public async Task<int> GetCount()
        {
            var totalRecords =myDbContext.EMR.Where(e=>e.Deadline>DateTime.Now).Count(); // 获取数据总量
            return totalRecords;
        }
        public async Task<List<EMR>> GetList(int page, int pageSize)
        {
            var chances =myDbContext.EMR.Where(e => e.Deadline > DateTime.Now).Skip((page - 1) * pageSize).Take(pageSize).ToList();
            return chances;
        }

    }
}
